package cn.hwadee.muying.config;

import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.servlet.MultipartConfigElement;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mankind on 2018/8/8.
 */
@Configuration
public class FileConfig extends WebMvcConfigurerAdapter {
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //  单个数据大小
        factory.setMaxFileSize("102400KB"); // KB,MB
        /// 总上传数据大小
        factory.setMaxRequestSize("102400KB");

        return factory.createMultipartConfig();
    }
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        converters.add(0,new ByteArrayHttpMessageConverter());
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter=new MappingJackson2HttpMessageConverter();
        List<MediaType> types=new ArrayList<>();
        types.add(MediaType.APPLICATION_JSON_UTF8);
        types.add(MediaType.APPLICATION_OCTET_STREAM);
        mappingJackson2HttpMessageConverter.setSupportedMediaTypes(types);
        converters.add(1,mappingJackson2HttpMessageConverter);
        StringHttpMessageConverter stringHttpMessageConverter=new StringHttpMessageConverter();
        List<MediaType> mediaTypes=new ArrayList<>();
        mediaTypes.add(MediaType.TEXT_PLAIN);
        stringHttpMessageConverter.setSupportedMediaTypes(mediaTypes);
        converters.add(2,stringHttpMessageConverter);
    }

}
